//引入axios
import axios from "axios";
//引入进度条
import nprogress from "nprogress";
import 'nprogress/nprogress.css'
//加载隐藏小球
nprogress.configure({ showSpinner:false })

//axios.create方法执行:返回也是axios
const request = axios.create({
    baseURL: '/mock',//请求基础路径
    timeout: 5000//超时的时间
});

//请求拦截器:只要发请求(发请求之前触发) config:配置对象
request.interceptors.request.use((config) => {
    nprogress.start();
    //需要把配置对象返回:最重要的一件事情,请求头携带公共参数
    return config
});

//响应拦截器
request.interceptors.response.use((res) => {
    nprogress.done();
    return res.data;
}, (error) => {
    const status = error.request.status;
    switch (status) {
        case 404:
            alert('请求失败');
            break;
        case 403:
            alert('未授权');
            break;
        case 500 || 501 || 502 || 503 || 504:
            alert('服务器BBQ');
            break;
    }
    return new Promise((resolve, reject) => {})
})
export default request;